package com.admin.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.admin.common.annotation.Log;
import com.admin.common.core.controller.BaseController;
import com.admin.common.core.domain.PageQuery;
import com.admin.common.core.domain.R;
import com.admin.common.core.domain.entity.SysDictType;
import com.admin.common.core.domain.vo.SysDictTypeQuery;
import com.admin.common.core.page.TableDataInfo;
import com.admin.common.enums.BusinessType;
import com.admin.common.utils.poi.ExcelUtil;
import com.admin.system.service.SysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict")
public class SysDictTypeController extends BaseController {

    private final SysDictTypeService dictTypeService;

    /**
     * 查询字典类型列表
     */
    @SaCheckPermission("system:dict:list")
    @PostMapping("/list")
    public TableDataInfo<SysDictType> list(@RequestBody SysDictTypeQuery dictType) {
        return dictTypeService.selectPageDictTypeList(dictType);
    }

    /**
     * 导出字典类型列表
     */
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:dict:export")
    @PostMapping("/export")
    public void export(SysDictTypeQuery dictType, HttpServletResponse response) {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil.exportExcel(list, "字典类型", SysDictType.class, response);
    }

    /**
     * 查询字典类型详细
     *
     * @param id 字典ID
     */
    @SaCheckPermission("system:dict:detail")
    @PostMapping(value = "/detail/{id}")
    public R<SysDictType> detail(@PathVariable Long id) {
        return R.ok(dictTypeService.selectDictTypeById(id));
    }

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:dict:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping(value = "add")
    public R<Void> add(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return R.fail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.insertDictType(dict);
        return R.ok();
    }

    /**
     * 修改字典类型
     */
    @SaCheckPermission("system:dict:update")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PostMapping(value = "update")
    public R<Void> edit(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return R.fail("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.updateDictType(dict);
        return R.ok();
    }

    /**
     * 删除字典类型
     *
     * @param ids 字典ID串
     */
    @SaCheckPermission("system:dict:delete")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{ids}")
    public R<Void> delete(@PathVariable Long[] ids) {
        dictTypeService.deleteDictTypeByIds(ids);
        return R.ok();
    }

    /**
     * 刷新字典缓存
     */
    @SaCheckPermission("system:dict:delete")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @PostMapping("/refreshCache")
    public R<Void> refreshCache() {
        dictTypeService.resetDictCache();
        return R.ok();
    }

    /**
     * 获取字典选择框列表
     */
    @PostMapping("/optionselect")
    public R<List<SysDictType>> optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return R.ok(dictTypes);
    }

    /**
     * 启/禁用
     */
    @SaCheckPermission("system:dict:toggle")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PostMapping("/toggle")
    public R<Void> toggle(@RequestBody SysDictType dict) {
        return toAjax(dictTypeService.toggle(dict));
    }
}
